package com.example.wupic.bestball;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class QuestionActivity extends AppCompatActivity {
    TextView question_title=null;
    TextView question_answer=null;
    JSONObject lib= (JSONObject) MyApplication.session.get("lib");//从session中获取 当前题库信息
    String question_current="1";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
         Intent intent=getIntent();
        question_current=   intent.getStringExtra("current_question_id");//取出当前题目id
        question_title= findViewById(R.id.question_title);//题干
        question_answer= findViewById(R.id.question_answer);//答案
        getQuestion();
    }

    public void getQuestion(){
        //调用volley
        try {
            String t_lib_id=lib.get("t_lib_id").toString();//题库id
            //题目id

            String url="http://www.wupicheng.com:7888/question/nextquestion?" +
                    "t_lib_id="+t_lib_id+"&t_question_id="+question_current+"";
            JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                     String success=   jsonObject.get("success").toString();
                     if(success.equals("true")){
                         JSONArray array1= (JSONArray)jsonObject.get("nextquestion");//获取到题目
                          JSONObject nextquestion= (JSONObject) array1.get(0);
                         //展示题目数据
                          question_title.setText(nextquestion.get("t_question_title").toString());
                          question_answer.setText(nextquestion.get("t_question_answer").toString());

                     }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            });
            MyApplication.requestQueue.add(jsonObjectRequest);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
